﻿using Microsoft.Cloud.API.Azure.Services;
using System.Threading.Tasks;
using System.Web.Http;

namespace Microsoft.Cloud.API.Azure.Controllers
{
    [RoutePrefix("api/{subscriptionId}/CloudServices")]
    public class CloudServicesController : ApiController
    {
        /// <summary>
        /// get cloud services
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get(string subscriptionId)
        {
            using (var computeService = new ComputeService(subscriptionId))
            {
                var result = await computeService.GetCloudServiceListAsync();

                return Ok(result);
            }
        }

        /// <summary>
        /// get deployment info
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <param name="cloudServiceName"></param>
        /// <param name="deploymentName"></param>
        /// <returns></returns>
        [Route("{cloudServiceName}/{deploymentName}")]
        public async Task<IHttpActionResult> GetDeployment(string subscriptionId, string cloudServiceName, string deploymentName)
        {
            var computeService = new ComputeService(subscriptionId); ;
            try
            {
                var response = await computeService.GetDeploymentAsync(cloudServiceName, deploymentName);

                return Ok(response);
            }
            finally
            {
                if (computeService != null)
                {
                    computeService.Dispose();
                }
            }
        }
    }
}
